var commentDomain = "127.0.0.1";
var commentUrl = "http://" + commentDomain;
var commentWebSocketUrl = "ws://" + commentDomain;
var utils, ajaxLayer;
if (typeof jQuery === "undefined") {
	throw new Error("jquery-confirm requires jQuery");
}
function loadJs(url, callback) {
	var script = document.createElement('script');
	script.type = "text/javascript";
	if (typeof (callback) != "undefined") {
		if (script.readyState) {
			script.onreadystatechange = function() {
				if (script.readyState == "loaded" || script.readyState == "complete") {
					script.onreadystatechange = null;
					callback();
				}
			}
		} else {
			script.onload = function() {
				callback();
			}
		}
	}
	script.src = url;
	document.body.appendChild(script);
}
loadJs('/js/layer/layer.js', function() {
	ajaxLayer = parent.layer || layer;
	utils = (function() {
		var preResult = function(result) {
			if (result.code == undefined) {
				result = JSON.parse(result);
			}
			switch (result.code) {
			case 403:
				ajaxLayer.msg("登录超时,即将返回登录界面", {
					time : 2000
				}, function() {
					var l = parent.location || location;
					// l.reload();
				});
				break;
			case 401:
				ajaxLayer.msg("您没有权限访问!");
				break;
			default:
				ajaxLayer.msg("系统异常，请稍候重试!");
				break;
			}
		}
		var postUrl = function(url, data, callBack) {
			var ajaxLayerIndex;
			$.ajax({
				url : commentUrl + url,
				type : "POST",
				timeout : 60000,
				data : data,
				dataType : "json",
				traditional : true,// 传统的方式来序列化数据
				beforeSend : function(xhr) {
					try {
						ajaxLayerIndex = ajaxLayer.load();
					} catch (e) {
					}
				},
				success : function(result) {
					callBack(result);
					try {
						ajaxLayer.close(ajaxLayerIndex);
					} catch (e) {
					}
				},
				error : function(result) {
					try {
						ajaxLayer.close(ajaxLayerIndex);
					} catch (e) {
					}
					var r = {
						code : result.status,
						msg : ''
					};
					preResult(r);
				}
			});
		};

		var getUrl = function(url, data, callBack) {
			var ajaxLayerIndex;
			$.ajax({
				url : commentUrl + url,
				type : "GET",
				timeout : 60000,
				data : data,
				dataType : "json",
				traditional : true,// 传统的方式来序列化数据
				beforeSend : function(xhr) {
					try {
						ajaxLayerIndex = ajaxLayer.load();
					} catch (e) {
					}
				},
				success : function(result) {
					callBack(result);
					try {
						ajaxLayer.close(ajaxLayerIndex);
					} catch (e) {
					}
				},
				error : function(result) {
					try {
						ajaxLayer.close(ajaxLayerIndex);
					} catch (e) {
					}
					var r = {
						code : result.status,
						msg : ''
					};
					preResult(r);
				}
			});
		};

		var ajaxUrl = function(url, data, type, callBack) {
			var ajaxLayerIndex;
			$.ajax({
				url : commentUrl + url,
				type : type,
				timeout : 60000,
				data : data,
				dataType : "json",
				traditional : true,// 传统的方式来序列化数据
				beforeSend : function(xhr) {
					try {
						ajaxLayerIndex = ajaxLayer.load();
					} catch (e) {
					}
				},
				success : function(result) {
					callBack(result);
					try {
						ajaxLayer.close(ajaxLayerIndex);
					} catch (e) {
					}
				},
				error : function(result) {
					try {
						ajaxLayer.close(ajaxLayerIndex);
					} catch (e) {
					}
					var r = {
						code : result.status,
						msg : ''
					};
					preResult(r);
				}
			});
		};

		var getXMLHttpRequest = function() {
			var xmlreq = null;
			if (window.XMLHttpRequest) { // 非IE
				xmlreq = new XMLHttpRequest();
			} else if (window.ActiveXObject) {// IE
				try {
					xmlreq = new ActiveXObject("Msxml2.HTTP");
				} catch (e) {
					try {
						xmlreq = new ActiveXObject("microsoft.HTTP");
					} catch (e) {
						alert("请升级你的浏览器，以便支持ajax！");
					}
				}
			}
			return xmlreq;
		};
		var getParameter = function(str, name) {
			if (str.indexOf(name) != -1) {
				var pos_start = str.indexOf(name) + name.length + 1;
				var pos_end = str.indexOf("&", pos_start);
				if (pos_end == -1) {
					return str.substring(pos_start);
				} else {
					return str.substring(pos_start, pos_end);
				}
			} else {
				return undefined;
			}
		};
		// 设置cookie
		var setCookie = function(c_name, value, day) {
			var exdate = new Date();
			exdate.setTime(exdate.getTime() + day * 24 * 60 * 60 * 1000);
			document.cookie = c_name + "=" + escape(value) + ((day == null) ? "" : ";expires=" + exdate.toGMTString());
		};

		// 读取cookie
		var getCookie = function(c_name) {
			if (document.cookie.length > 0) {
				var c_start = document.cookie.indexOf(c_name + "=");
				if (c_start != -1) {
					c_start = c_start + c_name.length + 1;
					var c_end = document.cookie.indexOf(";", c_start);
					if (c_end == -1)
						c_end = document.cookie.length
					return unescape(document.cookie.substring(c_start, c_end))
				}
			}
			return ""
		};

		// 删除cookie
		var delCookie = function(c_name) {
			var exp = new Date();
			exp.setTime(exp.getTime() - 1);
			var cval = this.getCookie(c_name);
			if (cval != null) {
				document.cookie = c_name + "=" + cval + ";expires=" + exp.toGMTString();
			}
		};
		var replaceSearch = function(search, param, value) {
			if (search) {
				var p = getParameter(search, param);
				if (p) {
					search = search.replace(param + "=" + p, param + "=" + value);
				} else {
					search += "&" + param + "=" + value;
				}
			} else {
				search += "?" + param + "=" + value;
			}
			return search;
		};
		return {
			getXMLHttpRequest : getXMLHttpRequest,
			postUrl : postUrl,
			getUrl : getUrl,
			ajaxUrl : ajaxUrl,
			getParameter : getParameter,
			setCookie : setCookie,
			getCookie : getCookie,
			delCookie : delCookie,
			replaceSearch : replaceSearch,
		}
	})();

	var b = sessionStorage.getItem("globalSocketStatus");
	if (b == 'false' || !b) {
		loadJs('/js/websocket.js');
	}
});
